/*
 * (c) 2014 Virtusa Corporation.
 * All Rights Reserved
 */
package ead.cs.service.impl;

import ead.cs.domain.Coordinates;
import ead.cs.domain.Payment;
import ead.cs.domain.Shipment;
import ead.cs.repository.ShipmentRepository;
import ead.cs.service.PaymentService;
import ead.cs.service.ShipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

@Service
public class ShipmentServiceImpl implements ShipmentService {

    @Autowired
    private ShipmentRepository shipmentRepository;

    @Autowired
    private PaymentService paymentService;

    @Override
    public Shipment save(Shipment shipment) {
        if (shipment == null || shipment.getPayment() == null || shipment.getPayment().getId() == null || shipment.getDeclaration() == null) {
            throw new RuntimeException("Shipment instance is not valid: " + shipment);
        }
        Payment payment = paymentService.findOne(shipment.getPayment().getId());
        System.out.println("Payment: " + payment);
        shipment.setPayment(payment);


        List<Coordinates> coordinateses = new LinkedList<Coordinates>();
        Coordinates coordinates = new Coordinates(37.772323,-122.214897);
        Coordinates coordinates1 = new Coordinates(21.291982, -157.821856);
        Coordinates coordinates2 = new Coordinates(-33.7969235, 150.9224326);
        coordinateses.add(coordinates);
        coordinateses.add(coordinates1);
        coordinateses.add(coordinates2);

        shipment.setRoute(coordinateses);

        Coordinates position = new Coordinates(37.772323, -122.214897);
        shipment.setPosition(position);


        return shipmentRepository.save(shipment);
    }

    @Override
    public List<Shipment> findAll() {
        return shipmentRepository.findAll();
    }

    @Override
    public Shipment findOne(String id) {
        return shipmentRepository.findOne(id);
    }

    public ShipmentRepository getShipmentRepository() {
        return shipmentRepository;
    }

    public void setShipmentRepository(ShipmentRepository shipmentRepository) {
        this.shipmentRepository = shipmentRepository;
    }

    @Override
    public Shipment findByPaymentId(String paymentId) {
        return shipmentRepository.findByPaymentId(paymentId);
    }
}
